const app = getApp()
var {Api} = require('../../api/api')
const network = require("../../api/network.js");
Page({
  data: {
    array: [],//学科选择
    index: 0,//学科默认选中角标
    swiper: [],
    show: false, //弹框是否出来
    timer: null,
    days: '000', //天数
    hours: '00', //小时数
    minutes: '00', //分钟数
    seconds: '00', //秒数
    yan: '', //头部颜色
    can: '', //参数设置
    type: '', //1是设置2是进群
    register: false, //是否授权
    shu: '', //消息数量
    phone: false, //手机号
    domain: Api.domain,
    shuatinum:0,//刷题数
    mokaonum:0,//模考数
  },
  
  /****切换学科*****/
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
    let index=e.detail.value;
    let disid=this.data.array[index].id;
    wx.setStorageSync('disidkey',disid);
    
    this.can();

    let shunum=this.data.can.real_line ? this.data.can.real_line : 0 + this.data.can.fictitious_line ? this.data.can.fictitious_line : 0;
    let monum=this.data.can.mk_line ?  this.data.can.mk_line  : 0
    let shunums=shunum
    this.setData({
      shuatinum: shunum
    })
    this.setData({
      mokaonum: monum
    })

  },
  //获取学科列表\\
  get_library_discipline_list() {
    wx.$request(wx.$api.library_discipline, {
    }).then(res => {
      
      this.setData({
        array: res
      });
      let disidi=0;
      disidi=res['0'].id;
      this.setData({
        index: 0
      });
      wx.setStorageSync('disidkey',disidi);
      console.log(res);    
    })
},

  /**
   * 页面跳转 jumpPage
   * @author zhaojin
   * @date 2022-11-09
   * @param e
   */
  jumpPage(e){
    if (!wx.getStorageSync('key')){
      this.setData({
        register:true
      })
      return false;
    }
    wx.$request(wx.$api.add_mk_line,{

    })
    console.log(e.currentTarget.dataset.url)
    wx.navigateTo({
      url:e.currentTarget.dataset.path
    })
  },
    //跳官网小程序
    togw(){
      wx.navigateToMiniProgram({
        appId: 'wxbf766cfc7cca4af7',
      })
    },
  onLoad(options) {
    console.log("55555555555666666666666");
    this.can();
    //获取学科数据
    this.get_library_discipline_list();
    if (options.a) {
      wx.$cache.set('pid', options.a)
    }
    this.banner()
    if (wx.$cache.get('can')) {
      this.setData({
        can: wx.$cache.get('can')
      })
      this.time()
    } else {
      this.can()
    }
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
  can() {

    wx.$http.get(wx.$get.member_info,{}).then(res => {
      console.log('我的', res)
      network.put('userinfo', res)
    })
    
    wx.$http.get(wx.$get.get_show, {}).then(res => {
      this.setData({
        can: res
      })
      console.log('显示555555555555555555参数',res)
      wx.$cache.set('can', res)
      this.time()
       console.log(res,'显示参数')
    })
  },
  goranking() {
    wx.navigateTo({
      url: '/pages/ranking/ranking',
    })
    // if( wx.$cache.get('key')){
    //   wx.navigateTo({
    //     url: '/pages/ranking/ranking',
    //   })
    // }else{
    //   this.setData({
    //     register:true
    //   })
    // }
  },
  time() {
    let kaotime = this.data.can.examination_time
    let year2 = Number(kaotime.split(' ')[0].split('-')[0])
    let month2 = Number(kaotime.split(' ')[0].split('-')[1])
    let strDate2 = Number(kaotime.split(' ')[0].split('-')[2])
    let hour2 = Number(kaotime.split(' ')[1].split(':')[0])
    let minutes2 = Number(kaotime.split(' ')[1].split(':')[1])
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    var hour = date.getHours();
    var minutes = date.getMinutes();
    var seconds = date.getSeconds();
    console.log(this.data.can.examination_time);
    // console.log(year, month, strDate, hour, minutes, seconds, '时间', year2, month2, strDate2, hour2, minutes2)
    if (year <= year2) {
      if (month == month2) {
        if (strDate == strDate2) {
          if (hour == hour2) {

          } else if (hour < hour2) {
            console.log('执行')
            this.setData({
              timer: setInterval(() => {
                this.leftTimer(year2, month2, strDate2, hour2, minutes2, 0)
              }, 1000)
            })
          }
        } else if (strDate < strDate2) {
          console.log('执行1')
          this.setData({
            timer: setInterval(() => {
              this.leftTimer(year2, month2, strDate2, hour2, minutes2, 0)
            }, 1000)
          })
        }
      } else {
        this.setData({
          timer: setInterval(() => {
            this.leftTimer(year2, month2, strDate2, hour2, minutes2, 0)
          }, 1000)
        })
      }
    }


  },
  banner() { //轮播图
    wx.$http.post(wx.$get.get_banner, {
      type: 1
    }).then(res => {
      // console.log(res,'轮播图')
      this.setData({
        swiper: res,
        yan: res[0].bg_color
      })
      wx.setNavigationBarColor({
        frontColor: '#ffffff',
        backgroundColor: res[0].bg_color,

      })
    })
  },
  animationfinish(e){
    let current=e.detail.current
let swiper=this.data.swiper
this.setData({
  yan: swiper[current].bg_color,
})
wx.setNavigationBarColor({
  frontColor:'#ffffff',

  backgroundColor: swiper[current].bg_color,
})
  },
  tiao(e) { //轮播图跳转
    // console.log(e);
    var that = this;
    var looplist = that.data.swiper;
    var index = e.currentTarget.dataset.index;
    // console.log(looplist);
    if (looplist[index].target == "utside") {
      app.globalData.content = looplist[index].url;
      wx.navigateTo({
        url: '/pages/waparse/waparse?title=' + looplist[index].title + '&is_url=' + 2,
      })
    } else {
      if (looplist[index].applets_page_is_tabbar == 1) {
        // console.log(looplist[index].applets_page_path)
        wx.switchTab({
          url: looplist[index].applets_page_path,
        })
      } else if (looplist[index].target == 'self') {
        if (looplist[index].parameter) {
          wx.navigateTo({
            url: looplist[index].applets_page_path + '?' + looplist[index].parameter + '&' + looplist[index].applets_page_parameter,
          })
        } else {
          wx.navigateTo({
            url: looplist[index].applets_page_path,
          })
        }
      } else {
        wx.navigateToMiniProgram({
          appId: looplist[index].app_id, // 要跳转的小程序的appid
          path: looplist[index].path + '?' + looplist[index].parameter, // 跳转的目标页面
          extarData: {
            open: 'auth'
          },
          envVersion: 'release',
          success(res) {
            // 打开成功
          }
        })
      }
    }
  },
  leftTimer(year, month, day, hour, minute, second) { //计算倒计时的
    var leftTime = (new Date(year, month - 1, day, hour, minute, second)) - (new Date()); //计算剩余的毫秒数
    var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
    var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时
    var minutes = parseInt(leftTime / 1000 / 60 % 60, 10); //计算剩余的分钟
    var seconds = parseInt(leftTime / 1000 % 60, 10); //计算剩余的秒数
    days = checkTime2(days);
    hours = checkTime(hours);
    minutes = checkTime(minutes);
    seconds = checkTime(seconds);
    this.setData({
      days,
      hours,
      minutes,
      seconds
    })

    function checkTime(i) { //将0-9的数字前面加上0，例1变为01
      if (i < 10) {
        i = "0" + i;
      }
      return i;
    }

    function checkTime2(i) {
      if (i < 10) {
        i = "00" + i
      } else if (i < 100) {
        i = "0" + i
      }
      return i
    }
  },
  bindshe(e) { //弹框出来
    let {
      type
    } = e.currentTarget.dataset
    this.setData({
      show: true,
      type: type
    })
  },
  go(e) { //跳转页面
    let {
      url,
      flage,
      type,
      flag
    } = e.currentTarget.dataset
    // 显示进群
    if (flag) {
      wx.navigateTo({
        url,
      })
      return
    }
    if (type == 2) {
      this.setData({
        show: true,
        type: 6,
      })
      return
    }
    if (flage) {
      wx.navigateTo({
        url,
      })
    } else {
      wx.navigateTo({
        url,
      })
      // if (wx.$cache.get('key')) {
      //   wx.navigateTo({
      //     url,
      //   })
      // } else {
      //   this.setData({
      //     register: true
      //   })
      // }
    }
  },
  shu() { //用户消息提醒数
    wx.$http.get(wx.$get.member_news_num, {}).then(res => {
      // console.log(res,'用户消息提醒数')
      this.setData({
        shu: res
      })
    })
  },
  userinfo() { //我的
    var that = this
    var params = {

    }
    wx.$http.get(wx.$get.get_member_info, params).then(res => {
      if(!res.vip_time){
        res.vip_time='1970-01-01'
      }
      // console.log(res, '用户信息')
      wx.$cache.set('userinfo', res)
      wx.$cache.set('member_id', res.id)
      if (!res.mobile) {
        this.setData({
          phone: true
        })
      } else {
        this.setData({
          phone: false
        })
      }
    })
  },
  gojinsuo(){
    if(network.get('key')){
      wx.navigateTo({
        url: '/pages/bookList/bookList',
      })
    }else{
      this.setData({
        register:true
      })
    }
  },
  onShow() {
    if (wx.$cache.get('key')) {
      this.shu()
      this.userinfo()
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: wx.$cache.get('fx').title,
      imageUrl: wx.$cache.get('fx').img,
      path: `/pages/index/index?pid=${wx.$cache.get('member_id')}`
    }
  },
  onShareTimeline() {
    return {
      title: wx.$cache.get('fx').title,
      imageUrl: wx.$cache.get('fx').img,
      query: `a=${wx.$cache.get('member_id')}`,
    }
  }
})
